﻿namespace GoodAdmin.System;

/// <inheritdoc cref="ISysPositionService"/>
public class SysPositionService : DbRepository<SysPosition>, ISysPositionService, ITransient
{
    private readonly IRedisCacheManager _redisCacheManager;
    private readonly IDictDataService _dictDataService;

    public SysPositionService(IRedisCacheManager redisCacheManager, IDictDataService dictDataService)
    {
        _redisCacheManager = redisCacheManager;
        _dictDataService = dictDataService;
    }
    /// <inheritdoc/>
    public async Task AddPosition(PositionAddInput input)
    {
        await CheckInput(input);
        if (await InsertAsync(input))
            RefreshCache();
    }
    /// <inheritdoc/>
    public async Task UpdatePosition(PositionUpdateInput input)
    {
        await CheckInput(input);
        if (await InsertAsync(input))
        {
            RefreshCache();
        }            
    }

    /// <inheritdoc/>
    public async Task DeletePosition(List<BaseIdInput> input)
    {
        var idList = input.Select(x => x.Id).ToList();
        
        //判断是否有用户使用这些职位
        if (await Context.Queryable<SysUser>().AnyAsync(u => idList.Contains(u.PositionId)))
        {
            throw Oops.Oh(PositionErrorCode.P004);
        }
        if (await DeleteAsync(it => idList.Contains(it.Id)))
            RefreshCache();
    }

    /// <inheritdoc/>
    public async Task<List<SysPosition>> GetPositionList()
    {
        string key = CacheConst.SysPosition;
        var positionList = _redisCacheManager.Get<List<SysPosition>>(key);
        if (positionList == null)
        {
            positionList = await GetListAsync();
            _redisCacheManager.Set(key, positionList);
        }
        return positionList;
    }

    /// <inheritdoc/>
    public async Task<SqlSugarPagedList<SysPosition>> GetPositionPageList(PositionPageInput input)
    {        
        return await Context.Queryable<SysPosition>()
            .WhereIF(!string.IsNullOrWhiteSpace(input.Category),it=>it.Category==input.Category)
            .WhereIF(!string.IsNullOrWhiteSpace(input.PositionName),it=>it.Name.Contains(input.PositionName))
            .OrderBy(it=>it.SortCode)
            .ToPagedListAsync(input.PageIndex, input.PageSize);
    }

    #region 私有方法

    /// <summary>
    /// 检查输入
    /// </summary>
    /// <param name="position"></param>
    /// <param name="action"></param>
    /// <returns></returns>
    private async Task CheckInput(SysPosition position)
    {        
        var positionList = await GetPositionList();
        if (position.Id > 0 && !positionList.Any(p => p.Id == position.Id))
        {
            throw Oops.Bah(PositionErrorCode.P001);
        }
        var positionDictData = await _dictDataService.GetDictDataList("PosType");
        if (!positionDictData.Any(p => p.Name == position.Category))
        {
            throw Oops.Bah(PositionErrorCode.P005);
        }
        //相同分类下是否存在同名职位
        if (positionList.Any(p => p.Category==position.Category && p.Name==position.Name && p.Id!=position.Id))
        {
            throw Oops.Bah(PositionErrorCode.P003);
        }
        
    }

    //清除缓存    
    private void RefreshCache()
    {
        _redisCacheManager.Remove(CacheConst.SysPosition);
    }
    #endregion
}